package com.xw.userService.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xw.userService.dto.ResultDto;
import com.xw.userService.enums.ResultCodeEnum;
import com.xw.userService.enums.SystemEnum;
import com.xw.userService.mapper.UserInfoMapper;
import com.xw.userService.model.UserInfo;
import com.xw.userService.service.UserInfoService;
import com.xw.userService.utils.DateUtil;

/**
 * 用户接口实例
 * @author xiaoshengda
 *
 * @date 创建时间：2018年3月31日 下午12:06:28
 */
@Service
public class UserInfoServiceImp implements UserInfoService {
	
	private static final Logger LOG = LoggerFactory.getLogger(UserInfoServiceImp.class);
	@Autowired
	private UserInfoMapper userInfoMapper;
    /**
     * 用户新增
     */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResultDto<?>  insert(UserInfo userInfo) {
		ResultDto<?> resultDto=new ResultDto<>();
		try {
			userInfo.setCreateBy(SystemEnum.CREATE_BY.getCode());
			userInfo.setUpdateBy(SystemEnum.UPDATE_BY.getCode());
			userInfo.setCreateDate(DateUtil.getSystemDate());
			userInfo.setUpdateDte(userInfo.getUpdateDte());
			userInfo.setDelFlg(SystemEnum.DEL_FLG_N.getCode());
			this.userInfoMapper.insert(userInfo);
			resultDto.setCode(ResultCodeEnum.System.SUCCESS.getCode());
			resultDto.setMessage(ResultCodeEnum.System.SUCCESS.getMsg());
		} catch (Exception e) {
			resultDto.setCode(ResultCodeEnum.System.SYSTEM_ERROR.getCode());
			resultDto.setMessage(ResultCodeEnum.System.SYSTEM_ERROR.getMsg());
			LOG.debug("新增用户异常");
		}
		return resultDto;
	}
    /**
     * 删除用户
     */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void del(Long id) {
		this.userInfoMapper.del(id);
	}
    /**
     * 通过用户id
     */
	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public UserInfo loadByUserId(Long id) {
		return this.userInfoMapper.loadByUserId(id);
	}
    /**
     * 修改用户
     */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void update(UserInfo userInfo) {
		this.userInfoMapper.update(userInfo);
	}

}
